//
//  RHomeSearchBar.m
//  iMoive
//
//  Created by 侯伟 on 2021/8/26.
//

#import "RHomeSearchBar.h"

#import "RSearchViewController.h"
#import "RShareViewController.h"
#import "RHomeAllVideoViewController.h"
#import "RPlayHistoryViewController.h"

#import "ShareHanler.h"
@interface RHomeSearchBar ()

/// 搜索
@property(nonatomic,strong)UIButton* searchBtn;

/// 全部
@property(nonatomic,strong)UIButton* allBtn;

/// 历史
@property(nonatomic,strong)UIButton* histroyBtn;

/// 分享
@property(nonatomic,strong)UIButton* shareBtn;

@end

@implementation RHomeSearchBar

- (instancetype)init{
    if(self = [super initWithOrientation:MyOrientation_Horz]){
        self.backgroundColor = UIColor.whiteColor;
        self.padding = UIEdgeInsetsMake(0, 16, 0, 16 );
        self.myHorzMargin = 0;
        self.subviewHSpace = 8;
        [self initViews];
    }
    return self;
}

- (void)initViews{

    UIButton* searchBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [searchBtn setImage:[UIImage imageNamed:@"common_search_input"] forState:UIControlStateNormal];
    [searchBtn setTitle:@"点击搜索" forState:UIControlStateNormal];
    [searchBtn setTitleColor:COLOR_GRAY_BASE forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchMoive:) forControlEvents:UIControlEventTouchUpInside];
    searchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    searchBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 16);
    searchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    searchBtn.titleLabel.font = FONT_FIT(12);
    searchBtn.myCenterY = 0;
    searchBtn.myHeight = PX(30);
    searchBtn.layer.cornerRadius = PX(15);
    searchBtn.backgroundColor = COLOR_BACKGEOUND;
    searchBtn.weight = 1.0;
    [self addSubview:searchBtn];
    _searchBtn = searchBtn;
    
    UIButton* allBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    allBtn.backgroundColor = COLOR_BACKGEOUND;
    [allBtn setTitle:@"全部" forState:UIControlStateNormal];
    [allBtn setTitleColor:COLOR_GRAY_BASE forState:UIControlStateNormal];
    allBtn.titleLabel.font = FONT_FIT(12);
    [allBtn addTarget:self action:@selector(allMoive:) forControlEvents:UIControlEventTouchUpInside];
    allBtn.myCenterY = 0;
    allBtn.mySize = CGSizeMake(PX(50), PX(30));
    allBtn.layer.cornerRadius = PX(30)/2.0;
    [self addSubview:allBtn];
    _allBtn = allBtn;
    

    UIButton* historyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [historyBtn setImage:[UIImage imageNamed:@"common_history"] forState:UIControlStateNormal];
    [historyBtn addTarget:self action:@selector(historyMovie:) forControlEvents:UIControlEventTouchUpInside];
    historyBtn.myCenterY = 0;
    historyBtn.mySize = CGSizeMake(PX(24), PX(24));
    [self addSubview:historyBtn];
    _histroyBtn = historyBtn;
    
    UIButton* shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [shareBtn setImage:[UIImage imageNamed:@"home_share"] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareMovie:) forControlEvents:UIControlEventTouchUpInside];
    shareBtn.myCenterY = 0;
    shareBtn.mySize = CGSizeMake(PX(24), PX(24));
    [self addSubview:shareBtn];
    _shareBtn = shareBtn;
}

- (void)setVideoType:(VideoType)VideoType{
    _VideoType = VideoType;
    if(VideoType == VideoType_Hot){
        self.allBtn.visibility = MyVisibility_Gone;
        self.histroyBtn.visibility = MyVisibility_Visible;
        [self.histroyBtn sizeToFit];
        self.shareBtn.visibility = MyVisibility_Visible;
        [self.shareBtn sizeToFit];
    }else{
        self.allBtn.visibility = MyVisibility_Visible;
        [self.allBtn sizeToFit];
        self.histroyBtn.visibility = MyVisibility_Gone;
        self.shareBtn.visibility = MyVisibility_Gone;
    }
}

///搜索
- (void)searchMoive:(UIButton*)sender{
    [UIViewController showViewController:[RSearchViewController new] animated:YES];
}

/// 所有的
- (void)allMoive:(UIButton*)sender{
    RHomeAllVideoViewController* allMoive = [RHomeAllVideoViewController new];
    allMoive.VideoType = self.VideoType;
    [UIViewController showViewController:allMoive animated:YES];
}

/// 历史
/// @param sender -
- (void)historyMovie:(UIButton*)sender{
    [UIViewController showViewController:[RPlayHistoryViewController new] animated:YES];
}

/// 分享
/// @param sender -
- (void)shareMovie:(UIButton*)sender{
    
    [ShareHanler shareAPP];
}
@end
